//package com.wanmait.recruit.filter;
//
//import com.alibaba.druid.util.StringUtils;
//import com.wanmait.recruit.pojo.Admin;
//import com.wanmait.recruit.service.AdminService;
//import com.wanmait.recruit.util.CookieUtil;
//import com.wanmait.recruit.util.Md5Util;
//import org.springframework.context.ApplicationContext;
//import org.springframework.web.context.support.WebApplicationContextUtils;
//
//import javax.annotation.Resource;
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//
//@WebFilter("/manage/*")
//public class ManageAutoLoginFilter implements Filter {
//    public void destroy() {
//    }
//
//    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//        HttpServletRequest request = (HttpServletRequest)req;
//        HttpServletResponse response = (HttpServletResponse)resp;
//        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
//        AdminService adminService = (AdminService)ac.getBean("adminService");
//        HttpSession session = request.getSession();
//        Admin admin = (Admin) session.getAttribute("admin");
//        if(admin!=null) {
//            chain.doFilter(req, resp);
//            return;
//        }
//        String username = CookieUtil.getValue("adminName",request);
//        String password = CookieUtil.getValue("adminPass",request);
//        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password))
//        {
//            chain.doFilter(req, resp);
//            return;
//        }
//        admin = adminService.findAdminByName(username);
//        String rightPass = admin.getPassword();
//        rightPass = Md5Util.md5Super(rightPass);
//        if(rightPass.equals(password))
//        {
//            session.setAttribute("admin",admin);
//            CookieUtil.addCookie("adminName",username,"/",30*24*60*60,response);
//            CookieUtil.addCookie("adminPass",rightPass,"/",30*24*60*60,response);
//        }
//        else
//        {
//            CookieUtil.addCookie("adminName","","/",0,response);
//            CookieUtil.addCookie("adminPass","","/",0,response);
//        }
//        chain.doFilter(req,resp);
//    }
//
//    public void init(FilterConfig config) throws ServletException {
//
//    }
//
//}
